package com.auxgroup.smarthome.business.web.listener;

import com.auxgroup.smarthome.IserverInitService;
import com.auxgroup.smarthome.business.web.filter.PermissionBusinessMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class SystemInitListener implements IserverInitService {

    @Value("${server.contextPath}")
    protected String contextPath;

    @Autowired
    private PermissionBusinessMenu permissionUpmsMenu;

    private ConfigurableApplicationContext applicationContext;


    @Override
    public void appStartedOperate(ApplicationEvent event) {
        this.applicationContext = ((ApplicationReadyEvent) event).getApplicationContext();
//        RequestMappingHandlerMapping requestMapping = SpringContextHolder.getBean(RequestMappingHandlerMapping.class);
//        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMapping.getHandlerMethods();
//
//        handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
//                    Set<String> patterns = requestMappingInfo.getPatternsCondition().getPatterns();
//            System.out.println(Arrays.toString(handlerMethod.getMethodAnnotation(RequestMapping.class).method()));
//            System.out.println(handlerMethod.getBean()+"->"+handlerMethod.getMethod());
//            System.out.println(patterns.stream().findFirst().get());
//                });
//        permissionUpmsMenu.buildMenus();
//        initPermissionData();
    }

//    private void initPermissionData() {
//        List<Object[]> list = permissionUpmsMenu.buildMenus();
//        List<MenuEntity> menuEntities = new ArrayList<>();
//        list.stream().forEach(objects -> {
//            String url = objects[0].toString();
//            PermissionMenu permissionMenu = (PermissionMenu) objects[1];
//            String id = permissionMenu.id(),
//                    menuName = permissionMenu.name(),
//                    parentId = null == permissionMenu.parentId() || "".equals(permissionMenu.parentId()) ? null : permissionMenu.parentId();
//            MenuTypeEnum menuType = permissionMenu.menuType();
//            MenuEntity menuEntity = menuRepository.findOne(id);
//            if(null == menuEntity) {
//                menuEntity = new MenuEntity(id, menuName, parentId, menuType, url);
//            }
//            menuEntities.add(menuEntity);
//        });
//        menuRepository.save(menuEntities);
//    }


    @Override
    public void appClosedOperate(ApplicationEvent event) {
        System.err.println("demo: 此进程关闭后，清除此进程中所有缓存！！！");
    }


}
